package com.service.totalauth.rabbitmq;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.rabbitmq.client.Channel;
import com.service.config.utils.RedisUtil;
import com.service.totalauth.config.RabbitConfig;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.*;

/**
 * @ClassName: MsgReceiver
 * @Description:
 * @Author guochuang
 * @Date 2021/10/25
 * @Version 1.0
 */
@Component
@Slf4j
public class MsgReceiver {
    private final ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
    private final ExecutorService product = new ThreadPoolExecutor(10,20,200L, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<Runnable>(),namedThreadFactory);
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    RedisUtil redisUtil;
    @RabbitListener(queues = RabbitConfig.QUEUE_C)
    public void processC(Channel channel, String content, Message message) throws Exception {

        logger.info(">>>>>>>>>>>>>>>>>message：");
        String token = "";

        try {
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>>>>>error");
        }
    }

    /**
     * 监听死信队列
     * @param msg
     * @param channel
     * @param message
     * @throws IOException
     */
    @RabbitListener(queues = RabbitConfig.DEAD_QUEUE)
    public void listener_dead(String msg, Channel channel, Message message) throws IOException {
        System.out.println("死信接收到消息" + msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     *  监听队列
     * @param msg
     * @param channel
     * @param message
     * @throws IOException
     */
    @RabbitListener(queues = RabbitConfig.QUEUE)
    public void listener(String msg, Channel channel, Message message) throws IOException {
        log.info("当前时间:{},收到死信队列的消息:{}", new Date(), msg);
        //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
    }

}
